import wepy from 'wepy'
import api from '../api'

export default class commentCreate extends wepy.mixin {
  data = {

  }

  methods = {
    
  }
  //直接回复
  async directComment() {
    let data = {
      topic_key: this.options.topic_key,
      content: this.textinputValue
    }
    console.log("direct-comment, data: ", data);
    await wepy.showLoading({
      title: "发布中...",
      mask: true
    })
    let res = await api.createComment(data);
    wepy.hideLoading();
    console.log(`direct-comment, res: `, res);
    await wepy.showToast({
      title: res.msg,
      icon: res.ret === 0 ? "success" : "none"
    })
    if (res.ret === 0) {
      this.$wxpage.newComment = res.data.data[0];
      this.$broadcast('clear-input-value');
      await this.checkNewComment();
    }
  }
  //检查新回复并定位
  checkNewComment() {
    if (this.$wxpage.newComment) {
      //新发布了新回复，删除标识，追加到最底端
      console.log(`${this.$name} -- newComment`);
      let newComment = this.$wxpage.newComment;
      console.log(newComment);
      //添加尾部，不定位
      this.list.push(newComment);
      //滚动到底部
      let newCommentId = newComment.id;
      //要先刷新页面
      this.$apply(() => {
        setTimeout(() => {
          var query = wx.createSelectorQuery();
          //目标元素的信息
          query.select('#comment-box').boundingClientRect();
          //视图的信息
          query.selectViewport().boundingClientRect();
          query.exec( res => {
            console.log(res);
            this.$wxpage.newComment = null;
            wx.pageScrollTo({
              scrollTop: res[0].height
            })
          })
        }, 300)
        
      })
    }
  }
  
}
